package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.InventoryImportDTO;
import com.yami.shop.bean.dto.ProdInventoryDTO;
import com.yami.shop.bean.model.ProdInventory;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.FileUtil;
import com.yami.shop.service.ProdInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品详细库存Controller
 */
@RestController
@RequestMapping("/prod/inventory")
@Api(tags = "商品详细库存接口")
@Slf4j
public class ProdInventoryController {

    @Autowired
    private ProdInventoryService prodInventoryService;

//    @Autowired
//    private FileUtil fileUtil;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询商品详细库存列表")
    public ResponseEntity<IPage<ProdInventoryDTO>> pageProdInventory(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("商品ID") @RequestParam(required = false) Long prodId,
            @ApiParam("店铺ID") @RequestParam(required = false) Long shopId,
            @ApiParam("分类ID") @RequestParam(required = false) Long categoryId,
            @ApiParam("状态(0-已售出 1-未售出 2-锁卡状态)") @RequestParam(required = false) Integer status) {
        try {
            Page<ProdInventoryDTO> page = new Page<>(pageNum, pageSize);
            IPage<ProdInventoryDTO> result = prodInventoryService.pageProdInventory(page, prodId, shopId, categoryId, status);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("分页查询商品详细库存列表失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取商品详细库存详情")
    public ResponseEntity<ProdInventoryDTO> getProdInventory(@PathVariable("id") Long id) {
        try {
            ProdInventoryDTO prodInventoryDTO = prodInventoryService.getProdInventoryById(id);
            return ResponseEntity.ok(prodInventoryDTO);
        } catch (Exception e) {
            log.error("获取商品详细库存详情失败", e);
            return ResponseEntity.badRequest().build();
        }
    }
//
//    @PostMapping("/add")
//    @ApiOperation(value = "添加商品详细库存")
//    public ResponseEntity<Void> addProdInventory(@Valid @RequestBody ProdInventory prodInventory) {
//        try {
//            prodInventoryService.addProdInventory(prodInventory);
//            return ResponseEntity.ok().build();
//        } catch (YamiShopBindException e) {
//            log.error("添加商品详细库存失败: {}", e.getMessage());
//            return ResponseEntity.badRequest().build();
//        } catch (Exception e) {
//            log.error("添加商品详细库存失败", e);
//            return ResponseEntity.badRequest().build();
//        }
//    }

    @PostMapping("/batch-add")
    @ApiOperation(value = "批量添加商品详细库存")
    public ResponseEntity<Void> batchAddProdInventory(@Valid @RequestBody InventoryImportDTO inventoryImportDTO) {
        try {
            prodInventoryService.batchAddProdInventory(inventoryImportDTO);
            return ResponseEntity.ok().build();
        } catch (YamiShopBindException e) {
            log.error("批量添加商品详细库存失败: {}", e.getMessage());
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("批量添加商品详细库存失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新商品详细库存")
    public ResponseEntity<Void> updateProdInventory(@Valid @RequestBody ProdInventory prodInventory) {
        try {
            prodInventoryService.updateProdInventory(prodInventory);
            return ResponseEntity.ok().build();
        } catch (YamiShopBindException e) {
            log.error("更新商品详细库存失败: {}", e.getMessage());
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("更新商品详细库存失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品详细库存")
    public ResponseEntity<Void> deleteProdInventory(@PathVariable("id") Long id) {
        try {
            prodInventoryService.deleteProdInventory(id);
            return ResponseEntity.ok().build();
        } catch (YamiShopBindException e) {
            log.error("删除商品详细库存失败: {}", e.getMessage());
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("删除商品详细库存失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation(value = "批量删除商品详细库存")
    public ResponseEntity<Void> batchDeleteProdInventory(@RequestBody List<Long> ids) {
        try {
            prodInventoryService.batchDeleteProdInventory(ids);
            return ResponseEntity.ok().build();
        } catch (YamiShopBindException e) {
            log.error("批量删除商品详细库存失败: {}", e.getMessage());
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("批量删除商品详细库存失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/parseTxtFile")
    @ApiOperation(value = "解析TXT文件", notes = "上传并解析TXT文件内容，返回文件中的行数据")
    public ResponseEntity<List<String>> parseTxtFile(
            @ApiParam(value = "TXT文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "是否跳过第一行", defaultValue = "false") @RequestParam(value = "skipFirstLine", defaultValue = "false") boolean skipFirstLine) {
        
        List<String> lines = FileUtil.readTxtFileLinesList(file, skipFirstLine);
        return ResponseEntity.ok(lines);
    }

} 